package com.gy.studyonlineapp.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gy.studyonlineapp.R;
import com.gy.studyonlineapp.adapter.CollectItemAdapter;
import com.gy.studyonlineapp.common.Constants;
import com.gy.studyonlineapp.constants.SharedPreference;
import com.gy.studyonlineapp.entity.CollectionItem;
import com.gy.studyonlineapp.entity.Question;
import com.gy.studyonlineapp.entity.User;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyCollectActivity extends AppCompatActivity {

    private ListView listView;

    private List<CollectionItem> lists = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mycollect);
        listView = findViewById(R.id.collect_list_view);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
        initData();
    }

    private void initData() {
        SharedPreferences sp = getSharedPreferences(SharedPreference.SHARED, Context.MODE_PRIVATE);
        String userJson = sp.getString("user", null);
        Gson gson = new Gson();
        User user = gson.fromJson(userJson, User.class);
        System.out.println(String.valueOf(user.getId()) + "------------------------");
        // 根据改用户ID进行查询数据库
        String url = "http://" + Constants.localhost + ":8080/search-collect-user";
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .add("userId", String.valueOf(user.getId()))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(e.getMessage() + "=============");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                List<Question> list = gson.fromJson(response.body().string(), new TypeToken<List<Question>>() {}.getType());
                for (int i = 0; i < list.size(); i++) {
                    CollectionItem item = new CollectionItem();
                    item.setContent(list.get(i).getContent());
                    item.setId(list.get(i).getId());
                    lists.add(item);
                    System.out.println(lists.get(i).getContent() + "-------------");
                }
                listView.setAdapter(new CollectItemAdapter(MyCollectActivity.this, R.layout.activity_collect_item, lists));
            }
        });
    }
}